﻿import socket
import json
port=23334
root="192.168.118.1"
down_server="192.168.118.130"

def udp_send(address,message):
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    client_socket.sendto(message, address) #将msg内容发送给指定接收方
    client_socket.close();
    return

sendjson1=json.dumps({"order":"send_aggregation_request"})
sendjson2=json.dumps({"order":"test_local_model"})
sendjson3=json.dumps({"order":"read_model"})
sendjson4=json.dumps({"order":"save_model"})
sendjson5=json.dumps({"order":"reset_model"})
running=True
order={"1":"子节点发出聚合请求",
       "2":"子节点测试本地模型",
       "3":"根节点测试本地模型",
       "4":"子节点读取本地模型",
       "5":"根节点读取本地模型",
       "6":"子节点保存本地模型",
       "7":"根节点保存本地模型",
       "8":"子节点重置本地模型",
       "9":"根节点重置本地模型",
       "0":"退出"}

for i in order:
    print(i,order[i])

while running:
    a = input("输入命令序号:")
    if a=="1":
        udp_send((down_server,port),sendjson1.encode('utf-8'))
    elif a=="2":
        udp_send((down_server,port),sendjson2.encode('utf-8'))
    elif a=="3":
        udp_send((root,port),sendjson2.encode('utf-8'))
    elif a=="4":
        udp_send((down_server,port),sendjson3.encode('utf-8'))
    elif a=="5":
        udp_send((root,port),sendjson3.encode('utf-8'))
    elif a=="6":
        udp_send((down_server,port),sendjson4.encode('utf-8'))
    elif a=="7":
        udp_send((root,port),sendjson4.encode('utf-8'))
    elif a=="8":
        udp_send((down_server,port),sendjson5.encode('utf-8'))
    elif a=="9":
        udp_send((root,port),sendjson5.encode('utf-8'))
    elif a=="0":
        running=False